package com.tedu.ds.base.config;

import io.lettuce.core.ClientOptions;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.resource.ClientResources;
import io.lettuce.core.resource.DefaultClientResources;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.Serializable;

@Configuration
@ComponentScan(basePackages = {"com.tedu.ds.base.utils"})
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Serializable> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(RedisSerializer.json());

        redisTemplate.setHashKeySerializer(RedisSerializer.json());
        redisTemplate.setHashValueSerializer(RedisSerializer.json());

        return redisTemplate;
    }



    @Bean
    public ClientResources clientResources() {
        return DefaultClientResources.builder().build();
    }
    @Bean
    public LettuceClientConfiguration clientConfiguration(ClientResources clientResources) {
        GenericObjectPoolConfig<?> poolConfig = genericObjectPoolConfig();
        return LettucePoolingClientConfiguration.builder()
                .poolConfig(poolConfig)
                .clientOptions(ClientOptions.builder().build())
                .clientResources(clientResources)
                .build();
    }
    @Bean
    public RedisConnectionFactory redisConnectionFactory(LettuceClientConfiguration clientConfig) {
        RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration("localhost", 6379);
        return new LettuceConnectionFactory(standaloneConfig, clientConfig);
    }
    @Bean
    public GenericObjectPoolConfig<?> genericObjectPoolConfig() {
        GenericObjectPoolConfig<?> poolConfig = new GenericObjectPoolConfig<>();
        poolConfig.setMaxIdle(50); // 最大空闲连接数
        poolConfig.setMinIdle(10); // 最小空闲连接数
        poolConfig.setMaxTotal(100); // 最大连接数
        poolConfig.setMaxWaitMillis(2000); // 获取连接的最大等待时间
        poolConfig.setTestOnBorrow(true); // 借用连接时测试连接的有效性
        return poolConfig;
    }
}
